#include "checktable.h"
#include "ui_checktable.h"
#include "util.h"
#include "checktableitem.h"
#include "model/datacenter.h"
#include "toast.h"

CheckTable::CheckTable(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CheckTable)
{
    ui->setupUi(this);

    ui->videoStatus->addItem("全部分类");
    ui->videoStatus->addItem("待审核");
    ui->videoStatus->addItem("审核通过");
    ui->videoStatus->addItem("审核驳回");
    ui->videoStatus->addItem("已下架");
    ui->videoStatus->addItem("转码中");
    ui->videoStatus->setCurrentIndex(0);

    paginator = new Paginator(20, ui->paginatorArea);
    paginator->move(0,15);

    // 限制编辑框只能输入手机号
    // 此处使⽤正则表达式对视频⽤⼾id进⾏校验
    // 视频⽤⼾id是服务端⽣成，格式为：7d98-a3846253-0003
    // QRegularExpression 是Qt中⽤于处理正则表达式的类
    QRegularExpression regExp("^[0-9a-f]{4}-[0-9a-f]{8}-[0-9a-f]{4}$");
    QValidator* validator = new QRegularExpressionValidator(regExp, this);
    ui->userIdEdit->setValidator(validator);


    connect(ui->resetBtn, &QPushButton::clicked, this, &CheckTable::onRestBtnClicked);
    connect(ui->queryBtn, &QPushButton::clicked, this, &CheckTable::onQueryBtnClicked);

    // 获取用户视频列表成功
    auto dataCenter = model::DataCenter::getInstance();
    connect(dataCenter, &model::DataCenter::getUserVideoListDone, this, &CheckTable::updateCheckTable);

    // 获取状态视频列表成功
    connect(dataCenter, &model::DataCenter::getStatusVideoListDone, this, [=](){
        updateCheckTable("", "checkPage");
    });
}

CheckTable::~CheckTable()
{
    delete ui;
}

void CheckTable::updateCheckTable(const QString& userId, const QString whichPage)
{
    if("checkPage" != whichPage){
        return;
    }

    // 清空界面上的旧视频内容
    QLayoutItem* item = nullptr;
    while(nullptr != (item = ui->layout->takeAt(0))){
        delete item->widget();
        delete item;
    }

    // 将DataCenter中保存到获取到的视频列表中的视频更新到界面
    auto dataCenter = model::DataCenter::getInstance();
    auto statusVideoList = dataCenter->getStatusVideoList();
    if(nullptr == statusVideoList){
        return;
    }

    // 如果获取的是第一页的视频，需要根据该种条件下视频总数 以及 一个页显示的视频总数，需要重新计算分页器上总的页数
    int totalVideoCount = statusVideoList->getVideoTotalCount();
    int videoCountOfPage = model::VideoList::PAGE_COUNT;
    if(1 == page){
        // 视频总数：40   每页显示：20   页数：(40+20-1)/20 = 2
        // 视频总数：39   每页显示：20   页数：(39+20-1)/20 = 2
        resetPaginator((totalVideoCount+videoCountOfPage-1)/videoCountOfPage);
    }

    auto videoList = statusVideoList->videoInfos;
    for(int i = 0; i < videoList.size(); ++i){
        CheckTableItem* videoItem = new CheckTableItem(this, videoList[i]);
        ui->layout->addWidget(videoItem);
    }
}

void CheckTable::getVideoList(int page)
{
    this->page = page;

    // 先清空DataCenter中保存的原有的旧视频，然后再保存新page中的视频
    // 因为在视频审核页面，界面中使用都加载一页的视频，无需缓存其他页面的视频
    auto dataCenter = model::DataCenter::getInstance();
    auto statusVideoList = dataCenter->getStatusVideoList();
    statusVideoList->videoInfos.clear();

    // 如果用户id编辑框不空，优先按照用户id获取视频列表
    // 如果用户id编辑框是空，再按照视频状态获取视频列表
    QString userId = ui->userIdEdit->text();
    if(!userId.isEmpty()){
        // 获取指定用户视频列表
        dataCenter->getUserVideoListAsync(userId, page, "checkPage");
    }else{
        // 获取状态视频列表
        model::VideoStatus videoStatus = static_cast<model::VideoStatus>(ui->videoStatus->currentIndex());
        dataCenter->getStatusVideoListAsync(videoStatus, page);
    }
}

void CheckTable::resetPaginator(int pageCount)
{
    // 因为总页数发生变化之后，分页器上显示的页数需要重新更新，因此可以先将旧的分页器销毁掉重新创建
    if(paginator){
        delete paginator;
    }

    paginator = new Paginator(pageCount, ui->paginatorArea);
    paginator->move(0, 15);
    paginator->show();

    connect(paginator, &Paginator::pageChanged, this, [=](int page){
        getVideoList(page);
    });
}

void CheckTable::onRestBtnClicked()
{
    ui->resetBtn->setStyleSheet("background-color:#3ECEFF;"
                                "border-radius:4px;"
                                "font-family:微软雅⿊;"
                                "font-size:14px;"
                                "color:#FFFFFF;");
    ui->queryBtn->setStyleSheet("background:#FFFFFF;"
                                "border-radius:4px;"
                                "border:1px solid #DCDEE0;"
                                "font-family:微软雅⿊;"
                                "font-size:14px;"
                                "color:#222222;");
    // 清空⽤⼾id
    ui->userIdEdit->setText("");
    ui->videoStatus->setCurrentIndex(0);

    // 获取视频列表
    auto dataCenter = model::DataCenter::getInstance();
    auto myselfInfo = dataCenter->getMyselfInfo();
    if(myselfInfo->isAdminDisable()){
        Toast::showMessage("您已经被禁用，无法进行操作!!!");
    }else{
        getVideoList(1);
    }
}

void CheckTable::onQueryBtnClicked()
{
    // 设置按钮样式
    ui->queryBtn->setStyleSheet("background-color:#3ECEFF;"
                                "border-radius:4px;"
                                "font-family:微软雅⿊;"
                                "font-size:14px;"
                                "color:#FFFFFF;");
    ui->resetBtn->setStyleSheet("background:#FFFFFF;"
                                "border-radius:4px;"
                                "border:1px solid #DCDEE0;"
                                "font-family:微软雅⿊;"
                                "font-size:14px;"
                                "color:#222222;");

    // 获取视频列表
    auto dataCenter = model::DataCenter::getInstance();
    auto myselfInfo = dataCenter->getMyselfInfo();
    if(myselfInfo->isAdminDisable()){
        Toast::showMessage("您已经被禁用，无法进行操作!!!");
    }else{
        getVideoList(1);
    }
}
